package com.UnionPayCenter.account.service.impl;

import com.UnionPayCenter.account.annotation.PayService;
import com.UnionPayCenter.account.entity.dto.ChargeDTO;
import com.UnionPayCenter.account.entity.dto.PayDTO;
import com.UnionPayCenter.account.enums.PayChannelEnum;
import com.UnionPayCenter.account.manager.ChargeBillManager;
import com.UnionPayCenter.account.service.AccountService;
import com.UnionPayCenter.account.entity.ChargeBill;
import com.UnionPayCenter.entity.ShopInfoEntity;
import com.UnionPayCenter.entity.dto.ChargeResponse;
import com.UnionPayCenter.util.IdGen;
import org.springframework.beans.factory.annotation.Autowired;

@PayService(value = "wechatPayService", channel = PayChannelEnum.WECHAT_PAY)
public class WechatPayService implements com.UnionPayCenter.account.service.PayService {

    @Autowired
    private ChargeBillManager chargeBillService;

    @Autowired
    private AccountService accountService;

    @Override
    public void pay(PayDTO payDTO) {

        //chargeBillService.updateState(payDTO.getSn(), ChargeStatusEnum.SUCCESS);
        accountService.pay(payDTO.getAccountId(), payDTO.getAmount());
    }

    @Override
    public ChargeResponse charge(ChargeBill chargeBill, ChargeDTO chargeDTO, ShopInfoEntity shopInfo) {
        String merchantOrderId = IdGen.newStringId();
        chargeBill.setThirdSn(merchantOrderId);
        chargeBill.setSn(merchantOrderId);
        chargeBillService.saveChargeBill(chargeBill);

        ChargeResponse unionpayOrderResponse = new ChargeResponse();
        unionpayOrderResponse.setMerOrderId(merchantOrderId);
        unionpayOrderResponse.setSn(merchantOrderId);
        return unionpayOrderResponse;
    }
}
